package com.talent.common.config;

import cn.hutool.http.Header;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName：FeginConfig
 * @Author: hszhou
 * @Date: 2025/2/21 13:04
 * @Description: feign 配置 主要用于token的传递
 */
@Configuration
public class FeignConfig {


    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {

            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;

            if(attributes!=null){
                HttpServletRequest request = attributes.getRequest();

                Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                if(principal!=null){
                    requestTemplate.header(Header.AUTHORIZATION.name(), "bearer " + "token");
                }
            }

        };
    }
}
